﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Parallel3M.parallel3m.payload;
using System.Xml;

namespace Parallel3M.parallel3m.framework
{
    public class ParallelCycleProcessorController:ParallelProcessor
    {        
        public ParallelChannel readyChannel;
        public ParallelChannel completedChannel;
        public ParallelChannelSelector selector;

        public ArrayList cycleProcessors = new ArrayList();
        public long cycleLimit = 50;

        public override void process()
        {

            if (cycleProcessors.Count < cycleLimit)
            {
                foreach (ParallelConnection inputConnection in inputConnections)
                {
                    ParallelPayload payload = inputConnection.Flowout();
                    if (payload != null)
                    {
                        payloadcount++;

                        //handle the payload and generate new payload to the output connection
                        ParallelCycleProcessor cycleProcessor = new ParallelCycleProcessor();
                        cycleProcessor.id = Guid.NewGuid().ToString();
                        cycleProcessor.controller = this;
                        cycleProcessor.plGraph = (PayloadGraph)payload;
                        cycleProcessor.runtype = PROCESSORCONSTANT.RUNTYPE_THREAD;
                        cycleProcessor.ConnectChannelSelector(readyChannel, selector, completedChannel);
                        cycleProcessors.Add(cycleProcessor);
                        cycleProcessor.start();

                    }
                }
            }
        }

       
        public override void PostParse()
        {
            //readyChannel='C1' selector='S1' completedChannel='C3'
            String readyChannelName = (String)parseParameters["readyChannel"];
            readyChannel = (ParallelChannel)application.elements[readyChannelName];

            String selectorName = (String)parseParameters["selector"];
            selector = (ParallelChannelSelector)application.elements[selectorName];

            String completedChannelName = (String)parseParameters["completedChannel"];
            completedChannel = (ParallelChannel)application.elements[completedChannelName];
            
        }





    }
}
